/**
 * Created by DevilsEyes on 2016/4/22.
 */
'use strict';
let nodemailer = require('nodemailer');
let template = require('art-template');
let config = require('../config/config');
let fs = require('fs');
let localhost = config.localhost;

let MAIL_TEMPLATE = {};

['verifySuccess', 'notice']
    .forEach((path)=> {
        fs.readFile(`../midware/mailTemplate/${path}.html`, 'utf8', function (err, str) {
            if (err)throw err;
            MAIL_TEMPLATE[path] = template.compile(str);
        });
    });


let Host = {
    mail: 'bupt_gtwebsite@sina.com',
    pass: 'ah*2nv&83hj',
    smtp: 'smtp.sina.com'
};

let transporter = nodemailer.createTransport(
    {
        host: Host.smtp,
        secureConnection: true, // 使用 SSL
        tls: {
            rejectUnauthorized: false
        },
        port: 25, // SMTP 端口
        auth: {
            user: Host.mail,
            pass: Host.pass
        }
    }
);

module.exports = {

    sendVerifySuccessMail: (data)=> {

        return new Promise((O, X)=> {

            let _id = '' + data._id;
            let email = '' + data.email;
            let name = '' + data.name;

            if (!_id) {
                X({
                    code: 9999,
                    msg: '没有_id'
                });
            }

            if (!email) {
                X({
                    code: 9999,
                    msg: '没有邮箱'
                });
            }

            if (!name) {
                X({
                    code: 9999,
                    msg: '没有真实姓名'
                });
            }


            let mailOptions = {
                from: Host.mail,
                to: email,
                subject: `${name}同学您好，您在·图论课程网站·注册的账号已经审核通过了，请完整补充信息`,
                // text: '',
                html: MAIL_TEMPLATE['verifySuccess']({
                    name: name,
                    _id: _id,
                    localhost: localhost
                })
            };

            transporter.sendMail(mailOptions, function (error, info) {

                if (error) {
                    X({
                        code: 9999,
                        msg: '邮件发送错误,' + error.toString && error.toString()
                    });
                } else {
                    O();
                }
            });
        })
    },

    sendNoticeMail: (data)=> {

        return new Promise((O, X)=> {

            let email = '' + data.email;
            let name = '' + data.name;
            let title = '' + data.title;
            let article = '' + data.article;
            let created = +data.created;

            if (!title) {
                X({
                    code: 9999,
                    msg: '没有标题'
                });
            }

            if (!email) {
                X({
                    code: 9999,
                    msg: '没有邮箱'
                });
            }

            var t = new Date(created);

            let mailOptions = {
                from: Host.mail,
                to: email,
                subject: `${name}同学您好，图论课程老师发布了新通知`,
                // text: '',
                html: MAIL_TEMPLATE['notice']({
                    title: title,
                    article: article.split('\n') || [],
                    created: `${t.getFullYear()}-${t.getMonth() + 1}-${t.getDate()}`,
                    localhost: localhost
                })
            };

            transporter.sendMail(mailOptions, function (error, info) {

                if (error) {
                    X({
                        code: 9999,
                        msg: '邮件发送错误,' + error.toString && error.toString()
                    });
                } else {
                    O();
                }
            });
        })
    }
};